using MicroMsg.Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MicroMsg.Plugin
{
	public class PluginManager
	{
		private const string TAG = "PluginManager";
		public static List<PluginBase> mListPlugin = new List<PluginBase>();
		public static void reset()
		{
			PluginManager.mListPlugin.Clear();
		}
		public static bool addPlugin(PluginBase plugin)
		{
			if (plugin == null)
			{
				return false;
			}
			if (PluginManager.mListPlugin.Contains(plugin))
			{
				return false;
			}
			PluginBase pluginBase = PluginManager.findPluginByName(plugin.mName);
			if (pluginBase != null)
			{
				PluginManager.mListPlugin.Remove(pluginBase);
			}
			PluginManager.mListPlugin.Add(plugin);
			return true;
		}
		public static bool removePlugin(PluginBase plugin)
		{
			return plugin != null && PluginManager.mListPlugin.Remove(plugin);
		}
		public static List<PluginBase> getAllList()
		{
			return PluginManager.mListPlugin;
		}
		public static List<PluginBase> getListInTab(MainTabIndex index)
		{
			IEnumerable<PluginBase> enumerable = Enumerable.Where<PluginBase>(PluginManager.mListPlugin, (PluginBase plugin) => PluginBase.isInstalledPlugin(plugin) && plugin.isInTab(index));
			if (Enumerable.Count<PluginBase>(enumerable) <= 0)
			{
				return null;
			}
			return Enumerable.ToList<PluginBase>(enumerable);
		}
		public static List<PluginBase> getAllListInTab(MainTabIndex index)
		{
			IEnumerable<PluginBase> enumerable = Enumerable.Where<PluginBase>(PluginManager.mListPlugin, (PluginBase plugin) => plugin.isInTab(index));
			if (Enumerable.Count<PluginBase>(enumerable) <= 0)
			{
				return null;
			}
			return Enumerable.ToList<PluginBase>(enumerable);
		}
		public static List<PluginBase> getInstalledList()
		{
			IEnumerable<PluginBase> enumerable = Enumerable.Where<PluginBase>(PluginManager.mListPlugin, (PluginBase plugin) => PluginBase.isInstalledPlugin(plugin));
			if (Enumerable.Count<PluginBase>(enumerable) <= 0)
			{
				return null;
			}
			return Enumerable.ToList<PluginBase>(enumerable);
		}
		public static List<PluginBase> getUninstalledList()
		{
			IEnumerable<PluginBase> enumerable = Enumerable.Where<PluginBase>(PluginManager.mListPlugin, (PluginBase plugin) => !PluginBase.isInstalledPlugin(plugin));
			if (Enumerable.Count<PluginBase>(enumerable) <= 0)
			{
				return null;
			}
			return Enumerable.ToList<PluginBase>(enumerable);
		}
		public static PluginBase findPluginByName(string name)
		{
			IEnumerable<PluginBase> enumerable = Enumerable.Where<PluginBase>(PluginManager.mListPlugin, (PluginBase plugin) => plugin.mName == name);
			if (Enumerable.Count<PluginBase>(enumerable) <= 0)
			{
				return null;
			}
			if (Enumerable.Count<PluginBase>(enumerable) > 1)
			{
				Log.smethod_1("PluginManager", " more than one with same plugin , name = " + name);
			}
			return Enumerable.FirstOrDefault<PluginBase>(enumerable);
		}
		public static PluginBase findPluginByID(long long_0)
		{
			IEnumerable<PluginBase> enumerable = Enumerable.Where<PluginBase>(PluginManager.mListPlugin, (PluginBase plugin) => plugin.mID == long_0);
			if (Enumerable.Count<PluginBase>(enumerable) <= 0)
			{
				return null;
			}
			if (Enumerable.Count<PluginBase>(enumerable) > 1)
			{
				Log.smethod_1("PluginManager", " more than one with same plugin , id = " + long_0);
			}
			return Enumerable.FirstOrDefault<PluginBase>(enumerable);
		}
		public static PluginBase findPluginByUserName(string name)
		{
			IEnumerable<PluginBase> enumerable = Enumerable.Where<PluginBase>(PluginManager.mListPlugin, (PluginBase plugin) => plugin.mUserName == name);
			if (Enumerable.Count<PluginBase>(enumerable) <= 0)
			{
				return null;
			}
			if (Enumerable.Count<PluginBase>(enumerable) > 1)
			{
				Log.smethod_1("PluginManager", " more than one with mUserName plugin , name = " + name);
			}
			return Enumerable.FirstOrDefault<PluginBase>(enumerable);
		}
		public static void searchAllPlugin(Func<PluginBase, bool> predicate)
		{
			using (List<PluginBase>.Enumerator enumerator = PluginManager.mListPlugin.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					PluginBase current = enumerator.get_Current();
					if (PluginBase.isInstalledPlugin(current))
					{
						predicate.Invoke(current);
					}
				}
			}
		}
		public static void searchOnePlugin(Func<PluginBase, bool> predicate)
		{
			using (List<PluginBase>.Enumerator enumerator = PluginManager.mListPlugin.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					PluginBase current = enumerator.get_Current();
					if (PluginBase.isInstalledPlugin(current) && predicate.Invoke(current))
					{
						break;
					}
				}
			}
		}
	}
}
